package com.guli.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.gulimall.gulimallproduct.entity.BrandEntity;
import com.guli.gulimall.gulimallproduct.entity.CategoryEntity;
import com.guli.gulimall.gulimallproduct.service.BrandService;
import com.guli.gulimall.gulimallproduct.service.CategoryService;
import com.guli.gulimall.gulimallproduct.vo.BrandVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.gulimall.gulimallproduct.entity.CategoryBrandRelationEntity;
import com.guli.gulimall.gulimallproduct.service.CategoryBrandRelationService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author zouzhi
 * @email 1245268075@qq.com
 * @date 2022-04-03 10:19:09
 */
@RestController
@RequestMapping("gulimallproduct/categorybrandrelation")
@Api(value = "CategoryBrandRelationController接口", tags = "品牌分类关联接口")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    //    "/gulimallproduct/categorybrandrelation/brands/list"
    /**
     * 获取当前页面下的所有品牌
     */
    @GetMapping("/brands/list")
    @ApiOperation("获取分类关联的品牌")
    public  R getRelationBrandsList(@RequestParam(value = "catId",required = true) Long catId){
        List<BrandEntity> brandEntities = categoryBrandRelationService.getRelationBrandsListByCatId(catId);
        System.out.println(brandEntities);
        List<BrandVo> brandVos = brandEntities.stream().filter(Objects::nonNull).map((item) -> {
            BrandVo vo = new BrandVo();
            if(item!=null){
                vo.setBrandId(item.getBrandId());
                vo.setBrandName(item.getName());
                return vo;
            }
            return vo;
        }).collect(Collectors.toList());
        System.out.println(brandVos);
        return  R.ok().put("data",brandVos);
    }

    /**
     * 列表
     */
    @GetMapping("catelog/list")
    @ApiOperation("获取当前品牌关联的分类")
    public R list(@RequestParam Map<String, Object> params) {
        Long brandId = Long.valueOf((String) params.get("brandId"));
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.
                list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
//        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        /**
         * private Long brandId;
         * private Long catelogId;
         * 	prite String brandName;
         * 	prite String catelogName;
         */
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
